package com.mycat.common.util.encryption.hmac;

import java.security.NoSuchAlgorithmException;

import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.annotation.adapters.HexBinaryAdapter;

/**
 * Hash Message Authentication Code，散列消息鉴别码 基于密钥的Hash算法的认证协议。
 * 
 * @author Roger
 */
public class HMACUtil {

	/**
	 * 构建HmacMD5密钥
	 * 
	 * @return 
	 * @throws Exception 
	 */
	public static byte[] initHmacMD5Key() throws Exception {
		// 初始化HmacMD5摘要算法的密钥产生器
		KeyGenerator generator = KeyGenerator.getInstance("HmacMD5");
		// 产生密钥
		SecretKey secretKey = generator.generateKey();
		// 获得密钥
		byte[] key = secretKey.getEncoded();
		return key;
	}
	

	/**
	 * 生成32位HmacMD5加密数据
	 * @param data
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static String encodeHmacMD5(byte[] data, byte[] key)
			throws Exception {
		// 还原密钥
		SecretKey secretKey = new SecretKeySpec(key, "HmacMD5");
		// 实例化Mac
		Mac mac = Mac.getInstance(secretKey.getAlgorithm());
		// 初始化mac
		mac.init(secretKey);
		// 执行消息摘要
		byte[] digest = mac.doFinal(data);
		return new HexBinaryAdapter().marshal(digest);// 转为十六进制的字符串
	}

	/**
	 * 构建HmacSHA1密钥
	 * @return
	 * @throws NoSuchAlgorithmException
	 */
	public static byte[] initHmacSHAKey() throws NoSuchAlgorithmException {
		// 初始化HmacMD5摘要算法的密钥产生器
		KeyGenerator generator = KeyGenerator.getInstance("HmacSHA1");
		// 产生密钥
		SecretKey secretKey = generator.generateKey();
		// 获得密钥
		byte[] key = secretKey.getEncoded();
		return key;
	}

	/**
	 * 生成40位HmacSHA1加密数据
	 * @param data
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static String encodeHmacSHA(byte[] data, byte[] key)
			throws Exception {
		// 还原密钥
		SecretKey secretKey = new SecretKeySpec(key, "HmacSHA1");
		// 实例化Mac
		Mac mac = Mac.getInstance(secretKey.getAlgorithm());
		// 初始化mac
		mac.init(secretKey);
		// 执行消息摘要
		byte[] digest = mac.doFinal(data);
		return new HexBinaryAdapter().marshal(digest);// 转为十六进制的字符串
	}

	/**
	 * 构建HmacSHA256密钥
	 * @return
	 * @throws NoSuchAlgorithmException
	 */
	public static byte[] initHmacSHA256Key() throws NoSuchAlgorithmException {
		// 初始化HmacMD5摘要算法的密钥产生器
		KeyGenerator generator = KeyGenerator.getInstance("HmacSHA256");
		// 产生密钥
		SecretKey secretKey = generator.generateKey();
		// 获得密钥
		byte[] key = secretKey.getEncoded();
		return key;
	}

	/**
	 * 生成64位HmacSHA256加密数据
	 * @param data
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static String encodeHmacSHA256(byte[] data, byte[] key)
			throws Exception {
		// 还原密钥
		SecretKey secretKey = new SecretKeySpec(key, "HmacSHA256");
		// 实例化Mac
		Mac mac = Mac.getInstance(secretKey.getAlgorithm());
		// 初始化mac
		mac.init(secretKey);
		// 执行消息摘要
		byte[] digest = mac.doFinal(data);
		return new HexBinaryAdapter().marshal(digest);// 转为十六进制的字符串
	}

	/**
	 * 构建HmacSHA384密钥
	 * @return
	 * @throws NoSuchAlgorithmException
	 */
	public static byte[] initHmacSHA384Key() throws NoSuchAlgorithmException {
		// 初始化HmacMD5摘要算法的密钥产生器
		KeyGenerator generator = KeyGenerator.getInstance("HmacSHA384");
		// 产生密钥
		SecretKey secretKey = generator.generateKey();
		// 获得密钥
		byte[] key = secretKey.getEncoded();
		return key;
	}

	/**
	 * 生成96位HmacSHA384加密数据
	 * @param data
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static String encodeHmacSHA384(byte[] data, byte[] key)
			throws Exception {
		// 还原密钥
		SecretKey secretKey = new SecretKeySpec(key, "HmacSHA384");
		// 实例化Mac
		Mac mac = Mac.getInstance(secretKey.getAlgorithm());
		// 初始化mac
		mac.init(secretKey);
		// 执行消息摘要
		byte[] digest = mac.doFinal(data);
		return new HexBinaryAdapter().marshal(digest);// 转为十六进制的字符串
	}

	/**
	 * 构建HmacSHA512密钥
	 * @return
	 * @throws NoSuchAlgorithmException
	 */
	public static byte[] initHmacSHA512Key() throws NoSuchAlgorithmException {
		// 初始化HmacMD5摘要算法的密钥产生器
		KeyGenerator generator = KeyGenerator.getInstance("HmacSHA512");
		// 产生密钥
		SecretKey secretKey = generator.generateKey();
		// 获得密钥
		byte[] key = secretKey.getEncoded();
		return key;
	}

	/**
	 * 生成128位HmacSHA512加密信息
	 * @param data
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static String encodeHmacSHA512(byte[] data, byte[] key)
			throws Exception {
		// 还原密钥
		SecretKey secretKey = new SecretKeySpec(key, "HmacSHA512");
		// 实例化Mac
		Mac mac = Mac.getInstance(secretKey.getAlgorithm());
		// 初始化mac
		mac.init(secretKey);
		// 执行消息摘要
		byte[] digest = mac.doFinal(data);
		return new HexBinaryAdapter().marshal(digest);// 转为十六进制的字符串
	}

	public static void main(String[] args) throws Exception {
		String testString = "asdasd";

		byte[] keyHmacMD5 = initHmacMD5Key();
		System.out.println(encodeHmacMD5(testString.getBytes(), keyHmacMD5));
		System.out.println(encodeHmacMD5(testString.getBytes(), keyHmacMD5)
				.length());

		byte[] keyHmacSHA1 = initHmacSHAKey();
		System.out.println(encodeHmacSHA(testString.getBytes(), keyHmacSHA1));
		System.out.println(encodeHmacSHA(testString.getBytes(), keyHmacSHA1)
				.length());

		byte[] keyHmacSHA256 = initHmacSHA256Key();
		System.out.println(encodeHmacSHA256(testString.getBytes(),
				keyHmacSHA256));
		System.out.println(encodeHmacSHA256(testString.getBytes(),
				keyHmacSHA256).length());

		byte[] keyHmacSHA384 = initHmacSHA384Key();
		System.out.println(encodeHmacSHA384(testString.getBytes(),
				keyHmacSHA384));
		System.out.println(encodeHmacSHA384(testString.getBytes(),
				keyHmacSHA384).length());

		byte[] keyHmacSHA512 = initHmacSHA512Key();
		System.out.println(encodeHmacSHA512(testString.getBytes(),
				keyHmacSHA512));
		System.out.println(encodeHmacSHA512(testString.getBytes(),
				keyHmacSHA512).length());
	}
}
